<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../../assets/book.jpeg" alt="">
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="loginFormRules" label-width="0px" class="login_form">
        <!--用户名-->
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" @keyup.enter.exact.native="loginState()" />
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input ref="input" v-model="loginForm.password" prefix-icon="el-icon-lock" type="password" @keyup.enter.exact.native="loginState()" />
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="success" @click="pushToRegister()">注册</el-button>
          <el-button :plain="true" type="primary" @click="loginState(),clickHandler($event)">登录</el-button>
          <el-button type="info" @click="resetLoginForm(),clickHandler($event)">重置</el-button>
          <router-link to="/" style="float: right; margin-top: 40px; font-size: 14px; ">暂不登录</router-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>

export default {
  beforeRouteLeave(to, from, next) {
    if (this.canvasFlag) {
      this.deleteCanvas()
    }
    next()
  },
  data() {
    return {
      // 这是登录表单的数据绑定对象
      loginForm: {
        username: 'sicheng',
        password: 'shiro'
      },
      // 表单验证对象
      loginFormRules: {
        username: [
          { required: true, message: '请输入登录名称', trigger: 'blur' },
          { min: 2, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur' }
        ]
      },
      canvasFlag: false
    }
  },
  created() {
    this.$moment.init()
    this.$moment.animate()
    this.canvasFlag = true
  },
  mounted() {
    this.$nextTick(() => {
      this.$refs.input.focus()
    })
  },
  methods: {
    // 表单实例对象，重置表单
    resetLoginForm() {
      // this.$router.push({ path: '/modifyPassword' })
      this.$refs.loginFormRef.resetFields()
    },
    deleteCanvas() {
      document.getElementById('wall-bg').remove()
      this.canvasFlag = false
    },
    pushToRegister() {
      this.$router.push({ path: '/register', name: 'Register' })
    },
    loginState() {
      this.$refs.loginFormRef.validate((valid) => {
        if (valid) {
          this.$store.dispatch('user/login', this.loginForm).then((resp) => {
            if (resp) resp.role === 'user' ? this.$router.push({ path: '/' }) : this.$router.push({ path: '/panelChild' }) // 登录成功之后重定向到首页
            else this.$message.error('登录失败，请检查账号和密码！')
          }).catch(err => {
            this.$message.error(err.message) // 登录失败提示错误
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.login_container {
    height: 100%;
    margin-top: -730px;
}

.login_box {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    box-shadow: 0 0 10px 0 rgb(0 33 79 / 11%);
    transform: translate(-50%,-50%);
}

.login_box .avatar_box {
  position: absolute;
    height: 130px;
    width: 130px;
    border:1px solid #eee;
    border-radius:50%;
    padding:10px;
    box-shadow:0 0 10px  #ddd;
    left: 50%;
    transform: translate(-50%,-50%);
    background-color: #eee;
}

.login_box .avatar_box img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
}

.btns {
    display: flex;
    justify-content: flex-end;
}

.login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
}
</style>

